<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Order;
use DB;

class OrderController extends Controller
{
    public function getSum($collection)
    {
        $sum = array_sum($collection->pluck('money')->toArray());
        return $sum;
    }


    public function order_List()
    {
        if(!Auth::check()){
            return redirect('auth/login');
        }
        
        $user = Auth::user();
        // dd($user->lastlogin);
      
        $order_group = Order::where('user_id',$user->user_id)->groupBy('order_sn')->get();
        
        $orders = Order::where('user_id',$user->user_id)->get();
        
        $data = $this->getAll($order_group,$orders);
        // $data['lastlogin'] = $user->lastlogin;
        // dd($data);

        
        return view('order_list',['user'=>$user,'orders'=>$orders,'data'=>$data]);

    }

    /**
     *  
     *
     **/
    public function getAll( $group, $orders)
    {

        $data = [];
        foreach ($group as $g) {
            $data[$g->order_sn] = $g->toArray();
            $data[$g->order_sn]['order'] = [];
            foreach ($orders as $o) {
                if ($g->order_sn == $o->order_sn) {
                    // dd($o);
                    $data[$g->order_sn]['order'][] = $o;
                }
                $data[$g->order_sn]['sum'] = $this->getSum(collect($data[$g->order_sn]['order']));
            }
        }
        return $data;
    }


    //取消订单
    public function cancelOrder($order_sn)
    {
        Order::where('order_sn',$order_sn)->delete();
        return back();
    }





    public function homePage()
    {

        if(!Auth::check()){
            return redirect('auth/login');
        }

        $user = Auth::user();
        
        $data['lastlogin'] = $user->lastlogin;
        $data['parttime'] = time() - $user->lastlogin;
        $homepage = Order::where('user_id',$user->user_id)->where('status',1)->get(['money']);
        // dd($homepage);
        $data['count'] = count(Order::where('user_id',$user->user_id)->where('status',1)->groupBy('order_sn')->get(['order_sn']));
        
        $data['money'] = $this->getSum($homepage);
        // dd($data);
        return view('homepage',['data'=>$data]);

    }



    public function getPay()
    {
      
        $pay = DB::table('orders')->where('user_id',Auth::user()->user_id)->where('status',0)->get();
        // dd($pay);
        if (!$pay) {
            return back()->withInput();
        }
        $sum = 0;
        foreach ($pay as $value) {
            $sum += $value->money;
        }
        // dd($sum);
        return view('pay',['pay'=>$pay,'sum'=>$sum]);
    }


    //支付提交
    public function postPay()
    {
        $order = new Order;
        $order_id = $order->select('order_id')->where('user_id',Auth::user()->user_id)->where('status', 0)->get()->toArray();
        //dd($order_id);
        foreach($order_id as $v){
            $arr[] = $v['order_id'];
        }
        $validate = mt_rand(1000, 9999);
        return $order->whereIn('order_id', $arr)->update(['validate'=>$validate, 'status'=>1]) ?  redirect('homepage') : 'fail';
    }

    protected function getOrder()
    {
        return DB::table('orders')->where('user_id',Auth::user()->user_id)->where('status', 0)->get();
    }

}
